#include <iostream>
#include <vector>
#include <unordered_map>
#include <algorithm>

using namespace std;

int maxEnvelopes(vector<vector<int>>& en) {
    sort(en.begin(), en.end(), [](vector<int>& v1, vector<int>& v2) {
        return v1[0] != v2[0] ? v1[0] < v2[0] : v1[1] > v2[1];
        });

    vector<int> ret;
    ret.push_back(en[0][1]);

    for (int i = 1; i < en.size(); ++i)
    {
        int b = en[i][1];
        if (b > ret.back())
        {
            ret.push_back(b);
        }
        else
        {
            if (b > ret.back())
            {
                ret.push_back(b);
            }
            else
            {
                int left = 0, right = ret.size() - 1;
                while (left < right)
                {
                    int mid = (left + right) / 2;
                    if (ret[mid] >= b) right = mid;
                    else left = mid + 1;
                }
                ret[left] = b;
            }
        }
    }

    return ret.size();
}

int main()
{
    vector<vector<int>> arr;
    arr.push_back({2,2});
    arr.push_back({2,3});
    arr.push_back({5,1});
    arr.push_back({6,4});
    arr.push_back({6,7});


    maxEnvelopes(arr);

	return 0;
}